<template>
  <div>
    <div class="flex flex-col h-full">
      <SearchContainer @search="getData()" @reset="() => formRef.resetFields()">
        <a-form ref="formRef" class="form-style-flex" :model="formState">
          <a-form-item label="" name="mcbh">
            <a-input v-model:value="formState.mcbh" class="w-72" placeholder="类别编号/类别名称" />
          </a-form-item>
        </a-form>
      </SearchContainer>
      <a-card class="mt-2 flex-1">
        <div class="mb-3">
          <BaseExport type="运维" name="挂号类别" code="00001097" :search-params="formState">导出</BaseExport>
          <!-- <a-button class="ml-2">导出</a-button> -->
        </div>
        <BaseTable
          column-code="00001097"
          fill
          :data-source="tableData"
          bordered
          :loading="loading"
          :pagination="{ total: page.total, current: page.current, pageSize: Number(page.pageSize) }"
          @change="onPageChange"
        >
          <template #bodyCell="{ column, record, index }">
            <template v-if="column.dataIndex == 'xh'">
              {{ index + 1 }}
            </template>
            <template v-if="column.dataIndex == 'isSelfReg'">
              <a-tag v-if="record.isSelfReg === '1'" color="blue">是</a-tag>
              <a-tag v-else color="blue">否</a-tag>
            </template>
            <template v-if="column.dataIndex == 'isRequiredDoctor'">
              <a-tag v-if="record.isRequiredDoctor === '1'" color="blue">是</a-tag>
              <a-tag v-else color="blue">否</a-tag>
            </template>
            <template v-if="column.dataIndex == 'mzorjzStr'">
              <a-tag v-if="record.mzorjz === 0" color="green">{{ record.mzorjzStr }}</a-tag>
              <a-tag v-if="record.mzorjz === 1" color="red">{{ record.mzorjzStr }}</a-tag>
            </template>
            <template v-if="column.dataIndex === 'ztflagStr'">
              <a-switch v-model:checked="record.ztflag" checked-value="0" un-checked-value="1" @change="onHandleDeactivate(record, $event)" />
            </template>
          </template>
        </BaseTable>
      </a-card>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import { post } from '@/utils/request'
import { getSystemConfig } from '@/utils/systemConfig'

const loading = ref(false)
const formRef = ref(null)
const formState = ref({})
const tableData = ref([])
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })

const getData = async () => {
  loading.value = true
  const { code, data, total } = await post('/operation/opex/getRegisterDictInfoList', {
    mcbh: formState.value.mcbh,
    pageNum: page.value.current,
    pageSize: page.value.pageSize
  })
  loading.value = false
  if (code === 0) {
    page.value.total = total
    tableData.value = data.map((item) => {
      item.ztflag = item.ztflag ? '1' : '0'
      return item
    })
  }
}
const onHandleDeactivate = async (item, number) => {
  item.ztflag = number
  const { code, msg } = await post('/operation/opex/doSaveRegisterDictInfo', item)
  if (code === 0) {
    message.success(msg)
    getData()
  }
}

const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  getData()
}
onMounted(() => {
  getData()
})
</script>
